from django.http import HttpResponse, JsonResponse
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from api.charts.ChartGenerate import ChartGenerate
from api.high_api import Event
from framework.syntacticSugar.EventEntiys import EventEntiys


class EventRun(APIView):
    def get(self, request:Request):
        """
        执行事件
        :param request:
        :return:
        """
        data = request.data
        entiys = data.get("event_entiy")
        entiys = EventEntiys(**entiys)
        entiys.self = ChartGenerate.getCharacter_name(entiys.self)
        entiys.involver = ChartGenerate.getCharacter_name(entiys.involver   )
        name = data.get("event_name")
        ret = Event.exec_event(event_name=name, enevt_entiys=entiys)
        return Response(ret)

class AuthMap(APIView):

    def get(self, request: Request):
        """
        API: 对应 auth/get
        :param request:
        :return:
        """
        datas = request.data

        username = datas.get("username")
        tigger = datas.get('method')
        event_entiys = datas.get('event_entiys')

        assert username is not None and tigger is not None

        chart = ChartGenerate.getCharacter_name(username)  # 利用用户传来的数据加载对象
        json = Event.ByAuthGetEvent(chart, tigger, event_entiys)
        print(json)

        return Response(json)
